package net.maku.propagate.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.framework.security.user.SecurityUser;
import net.maku.propagate.entity.PropagateInnovationStudioEntity;
import net.maku.propagate.query.PropagateInnovationStudioQuery;
import net.maku.propagate.vo.PropagateInnovationStudioVO;
import net.maku.propagate.dao.PropagateInnovationStudioDao;
import net.maku.propagate.service.PropagateInnovationStudioService;
import net.maku.system.service.SysOrgService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 创新工作室
 *
 * @author 阿沐 babamu@126.com
 * @since 1.0.0 2025-03-16
 */
@Service
@AllArgsConstructor
public class PropagateInnovationStudioServiceImpl extends BaseServiceImpl<PropagateInnovationStudioDao, PropagateInnovationStudioEntity> implements PropagateInnovationStudioService {

    private final SysOrgService sysOrgService;

    @Override
    public PageResult<PropagateInnovationStudioVO> page(PropagateInnovationStudioQuery query) {
        IPage<PropagateInnovationStudioEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));
        List<PropagateInnovationStudioVO> result = BeanUtil.copyToList(page.getRecords(), PropagateInnovationStudioVO.class);
        sysOrgService.setVisible(result);
        return new PageResult<>(result, page.getTotal());
    }

    private LambdaQueryWrapper<PropagateInnovationStudioEntity> getWrapper(PropagateInnovationStudioQuery query){
        LambdaQueryWrapper<PropagateInnovationStudioEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StringUtils.isNotEmpty(query.getStudioName()), PropagateInnovationStudioEntity::getStudioName, query.getStudioName())
                .like(StringUtils.isNotEmpty(query.getStudioCode()), PropagateInnovationStudioEntity::getStudioCode, query.getStudioCode())
                .like(StringUtils.isNotEmpty(query.getPrincipal()), PropagateInnovationStudioEntity::getPrincipal, query.getPrincipal())
                .eq(ObjectUtils.isNotEmpty(query.getTopOrNot()),PropagateInnovationStudioEntity::getTopOrNot, query.getTopOrNot());
        dataScopeWrapper(wrapper);
        return wrapper.orderByDesc(PropagateInnovationStudioEntity::getCreateTime);
    }

    @Override
    public void save(PropagateInnovationStudioVO vo) {
        PropagateInnovationStudioEntity entity = BeanUtil.copyProperties(vo, PropagateInnovationStudioEntity.class);
        entity.setOrgId(SecurityUser.getUser().getOrgId());
        entity.setStudioCode("CX"+System.currentTimeMillis());
        baseMapper.insert(entity);
    }

    @Override
    public void update(PropagateInnovationStudioVO vo) {
        PropagateInnovationStudioEntity entity = BeanUtil.copyProperties(vo, PropagateInnovationStudioEntity.class);
        updateById(entity);
    }

    @Override
    public void unmount(Long id, Integer status) {
        PropagateInnovationStudioEntity entity =new PropagateInnovationStudioEntity();
        entity.setId(id);
        entity.setStatus(status);
        baseMapper.updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

}